//
//  ChangeView.m
//  兼职页面
//
//  Created by kuiyuan liang on 2021/5/31.
//

#import "ChangeView.h"
#import "CommonUtil.h"

@interface ChangeView()

@property (strong, nonatomic) UIView * indexView;
@property (strong, nonatomic) UIButton * selectbtn;

@end

@implementation ChangeView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = RGB(255.0, 91.0, 117.0);
        self.layer.cornerRadius = self.height/2;
        self.clipsToBounds = YES;
        [self addSubview:self.indexView];
        [self addSubview:self.sortbtn1];
        [self addSubview:self.sortbtn2];
        [self btnClick:self.sortbtn1];
    }
    return self;
}
- (void)btnClick:(UIButton *)sender{
    self.selectbtn.selected = false;
    self.selectbtn = sender;
    self.selectbtn.selected = YES;
    [UIView animateWithDuration:0.25 animations:^{
        self.indexView.centerX = sender.centerX;
    }];
    
}


-(UIView *)indexView{
    if (!_indexView) {
        _indexView = [[UIView alloc] initWithFrame:CGRectMake(0, AD(1), self.sortbtn1.width-AD(2), self.height-AD(2))];
        _indexView.backgroundColor = [UIColor whiteColor];
        _indexView.layer.cornerRadius = _indexView.height/2;
    }
    return _indexView;
}

-(UIButton *)sortbtn1{
    if (!_sortbtn1) {
        _sortbtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sortbtn1 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_sortbtn1 setTitle:@"正序" forState:UIControlStateNormal];
        [_sortbtn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sortbtn1 setTitleColor:self.backgroundColor forState:UIControlStateSelected];
        _sortbtn1.frame = CGRectMake(0, 0, self.width/2, self.height);
    }
    return _sortbtn1;
}

-(UIButton *)sortbtn2{
    if (!_sortbtn2) {
        _sortbtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sortbtn2 addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_sortbtn2 setTitle:@"倒序" forState:UIControlStateNormal];
        [_sortbtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sortbtn2 setTitleColor:self.backgroundColor forState:UIControlStateSelected];
        _sortbtn2.frame = CGRectMake(_sortbtn1.right, 0, self.width/2, self.height);
    }
    return _sortbtn2;
}
@end
